//
//  ZhenYanTopView.m
//  TQY
//
//  Created by 甘世清 on 2024/10/10.
//

#import "ZhenYanTopView.h"

@implementation ZhenYanTopView

- (void)SQ_UIViewForSelfView{
    self.backgroundColor = UIColor.whiteColor;
    self.layer.cornerRadius = 20;
    [self addSubview:self.headerView];
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(44);
        make.left.equalTo(self).offset(24);
        make.top.equalTo(self).offset(20);
    }];
    [self addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headerView.mas_right).offset(8);
        make.centerY.equalTo(self.headerView).offset(-10);
    }];
    
    [self addSubview:self.timeLabel];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headerView.mas_right).offset(8);
        make.centerY.equalTo(self.headerView).offset(10);;
    }];
    
    [self addSubview:self.shareBtn];
    [self.shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(50);
        make.centerY.equalTo(self.headerView);
    }];
    
    [self addSubview:self.messageTextView];
    [self.messageTextView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.headerView);
            make.right.equalTo(self.mas_right).offset(-20);
        make.top.mas_equalTo(self.headerView.mas_bottom).offset(20);
    }];
    
    [self addSubview:self.loveBtn];
    [self.loveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.width.mas_equalTo(SCREEN_WIDETH/3.f);
        make.height.mas_equalTo(50);
        make.bottom.equalTo(self.mas_bottom);
    }];
   
    [self addSubview:self.collectBtn];
    [self.collectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(SCREEN_WIDETH/3.f);
        make.width.mas_equalTo(SCREEN_WIDETH/3.f);
        make.height.mas_equalTo(50);
        make.bottom.equalTo(self.mas_bottom);
    }];
    
    [self addSubview:self.pinglunBtn];
    [self.pinglunBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right);
        make.width.mas_equalTo(SCREEN_WIDETH/3.f);
        make.height.mas_equalTo(50);
        make.bottom.equalTo(self.mas_bottom);
    }];
    WS(weakSelf);
    self.messageTextView.showTextMenu = ^(CGRect startRect, CGRect endRect, NSRange selectRange, BOOL isSelectAll) {
        [weakSelf showMenuWithStartRect:startRect endRect:endRect isAll:isSelectAll];
    };
    [self.loveBtn setTouchBlock:^(SQCustomButton * _Nonnull button){
        [weakSelf SQ_likeBtn];
    }];
    [self.collectBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_collectBtn];
    }];
    [self.shareBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_fenxiang];
    }];
}
- (void)setMottoModel:(MottoModel *)mottoModel{
    _mottoModel = mottoModel;
    [self.headerView.headerImage sd_setImageWithURL:SQURLImage(self.mottoModel.portrait) placeholderImage:SQGetImage(@"1")];
    self.nameLabel.text = self.mottoModel.name;
    self.timeLabel.text = self.mottoModel.createTime;
    self.messageTextView.text = self.mottoModel.content;
    [self.messageTextView mas_updateConstraints:^(MASConstraintMaker *make){
        make.height.mas_equalTo(mottoModel.height+10);
    }];
    if (self.mottoModel.isCollect) {
        self.collectBtn.image = SQGetImage(@"shoucang-2");
    } else {
        self.collectBtn.image = SQGetImage(@"shoucang");
    }
    if (self.mottoModel.islike) {
        self.loveBtn.image = SQGetImage(@"dianzan-2");
    } else {
        self.loveBtn.image = SQGetImage(@"dianzan");
    }
    self.loveBtn.text = [NSString stringWithFormat:@"%d",mottoModel.likeCount];
    self.collectBtn.text = [NSString stringWithFormat:@"%d",mottoModel.collectCount];
    self.pinglunBtn.text = [NSString stringWithFormat:@"%d",mottoModel.commentCount];
    if (mottoModel.commentCount <=0) {
        self.pinglunBtn.text = SQNSLocalString(@"抢首评");
    }  
    [self layoutIfNeeded];
}
-(void)SQ_likeBtn{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:self.mottoModel.userid forKey:@"id"];
    if (self.mottoModel.islike) {
        [dic setObject:@"0" forKey:@"type"];
    } else {
        [dic setObject:@"1" forKey:@"type"];
    }
    [HttpTool post:@"motto/like" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.mottoModel.islike = !self.mottoModel.islike;
        if (self.mottoModel.islike) {
            self.loveBtn.image = SQGetImage(@"dianzan-2");
            self.mottoModel.likeCount ++;
        } else {
            self.loveBtn.image = SQGetImage(@"dianzan");
            self.mottoModel.likeCount--;
        }
        self.loveBtn.text = [NSString stringWithFormat:@"%d",self.mottoModel.likeCount];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
    
}

-(void)SQ_collectBtn{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:self.mottoModel.userid forKey:@"id"];
    if (self.mottoModel.isCollect) {
        [dic setObject:@"0" forKey:@"type"];
    } else {
        [dic setObject:@"1" forKey:@"type"];
    }
    [HttpTool post:@"motto/collect" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.mottoModel.isCollect = !self.mottoModel.isCollect;
        if (self.mottoModel.isCollect) {
            self.collectBtn.image = SQGetImage(@"shoucang-2");
            self.mottoModel.collectCount ++;
        } else {
            self.collectBtn.image = SQGetImage(@"shoucang");
            self.mottoModel.collectCount --;
        }
        self.collectBtn.text = [NSString stringWithFormat:@"%d",self.mottoModel.collectCount];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
    
}

-(void)SQ_fenxiang{
    NSString *textToShare = @"邀您一起赏言品文";
    UIImage *imageToShare = [UIImage imageNamed:@"logo"];
    NSURL *urlToShare = [NSURL URLWithString:@"https://apps.apple.com/app/id6736890354"];
    NSArray *activityItems = @[urlToShare,textToShare,imageToShare];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
     activityVC.excludedActivityTypes = @[UIActivityTypePostToVimeo ];
    UIActivityViewControllerCompletionWithItemsHandler myBlock = ^(NSString *activityType,BOOL completed,NSArray *returnedItems,NSError *activityError){
    };
     [[Manager SQ_getCurrentViewController] presentViewController:activityVC animated:YES completion:nil];
}

- (void)showMenuWithStartRect:(CGRect)startRect endRect:(CGRect)endRect isAll:(BOOL)isAll
{
    // 显示菜单栏
    SJPopMenuItem *copy = [SJPopMenuItem itemWithType:SJPopMenuItemCopy];
    SJPopMenuItem *selectAll = [SJPopMenuItem itemWithType:SJPopMenuItemSelectAll];
    NSMutableArray *items = [NSMutableArray array];
    [items addObject:copy];//复制
    if (!isAll) {
        [items addObject:selectAll]; //全选
    }
    SJPopMenu *menu = [SJPopMenu menu];
    if (isAll) {
        [menu showBy:self.messageTextView withItems:items];
    } else {
        [menu showBy:self.messageTextView startRect:startRect endRect:endRect withItems:items];
    }
    __weak typeof(self) weakSelf = self;
    menu.itemActions = ^(SJPopMenuItemType type, NSString *title) {
        switch (type) {
            case SJPopMenuItemCopy://复制
                [weakSelf coppy];
                break;
            case SJPopMenuItemWithdraw: //撤回
                
                break;
            case SJPopMenuItemDelete: //删除
                
                break;
            case SJPopMenuItemSelectAll: //全选
                weakSelf.messageTextView.selectedRange = NSMakeRange(0, weakSelf.messageTextView.text.length);
                break;
            default:
                break;
        }
    };
}
- (void)coppy
{
    UIPasteboard *pab = [UIPasteboard generalPasteboard];
      pab.string = [self.messageTextView.text substringWithRange:self.messageTextView.selectedRange];
      if (pab == nil) {
          [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"复制失败")];
      } else {
          [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"复制成功")];
      }
}
-(void)SQ_headerViewButtonClick{
    PeopleDetailViewController * vc = [[PeopleDetailViewController alloc]init];
    vc.uid = self.mottoModel.uid;
    [[Manager SQ_getCurrentViewController].navigationController pushViewController:vc animated:YES];
}
- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.font = FONT_MEDIUM(16);
        _nameLabel.textColor = UIColor.blackColor;
        _nameLabel.backgroundColor = UIColor.clearColor;
        _nameLabel.numberOfLines = 0;
    }
    return _nameLabel;
}

- (UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc]init];
        _timeLabel.font = FONT_REGULAR(14);
        _timeLabel.textColor = SQColor666666;
        _timeLabel.backgroundColor = UIColor.clearColor;
    }
    return _timeLabel;
}
- (HeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[HeaderView alloc]init];
        _headerView.headerImage.contentMode = UIViewContentModeScaleAspectFill;
        _headerView.clipsToBounds = YES;
        [_headerView.btn addTarget:self action:@selector(SQ_headerViewButtonClick) forControlEvents:UIControlEventTouchUpInside];
        _headerView.layer.cornerRadius = 22;
        _headerView.layer.masksToBounds = YES;
    }
    return _headerView;
}
- (SJCustomSelectTextView *)messageTextView{
    if (!_messageTextView) {
        _messageTextView = [[SJCustomSelectTextView alloc]init];
        _messageTextView.backgroundColor = UIColor.whiteColor;
        _messageTextView.layer.cornerRadius = 10;
        _messageTextView.layer.masksToBounds = YES;
        _messageTextView.font = FONT_BOLD(18);
        _messageTextView.textColor = SQColor333333;
        _messageTextView.editable = NO;
        _messageTextView.selectable = YES;
//        _messageTextView.userInteractionEnabled = NO;
    }
    return _messageTextView;
}
 

- (SQCustomButton *)loveBtn{
    if (!_loveBtn) {
        _loveBtn = [[SQCustomButton alloc]initType:SQCustomButtonLeftImageType imageSize:CGSizeMake(24, 24) midmargin:4];
        _loveBtn.image = SQGetImage(@"dianzan");
        _loveBtn.textColor = SQColor333333;
        _loveBtn.font = FONT_REGULAR(14);
        _loveBtn.text = @"0";
    }
    return _loveBtn;
}
- (SQCustomButton *)collectBtn{
    if (!_collectBtn) {
        _collectBtn = [[SQCustomButton alloc]initType:SQCustomButtonLeftImageType imageSize:CGSizeMake(24, 24) midmargin:4];
        _collectBtn.image = SQGetImage(@"shoucang");
        _collectBtn.textColor = SQColor333333;
        _collectBtn.font = FONT_REGULAR(14);
        _collectBtn.text = @"0";
    }
    return _collectBtn;
}
- (SQCustomButton *)shareBtn{
    if (!_shareBtn) {
        _shareBtn = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(24, 24) midmargin:0];
        _shareBtn.image = SQGetImage(@"fenxiang");
        _shareBtn.textColor = UIColor.clearColor;
        _shareBtn.font = FONT_REGULAR(14);
        
    }
    return _shareBtn;
}
- (SQCustomButton *)pinglunBtn{
    if (!_pinglunBtn) {
        _pinglunBtn = [[SQCustomButton alloc]initType:SQCustomButtonLeftImageType imageSize:CGSizeMake(24, 24) midmargin:4];
        _pinglunBtn.image = SQGetImage(@"pinglun");
        _pinglunBtn.textColor = SQColor333333;
        _pinglunBtn.font = FONT_REGULAR(14);
        _pinglunBtn.text = @"0";
    }
    return _pinglunBtn;
}


@end
